package com.job.client.auth.listener;

import com.alibaba.fastjson2.JSON;
import com.job.client.auth.mapper.ActionMapper;
import com.job.client.model.auth.pojo.Action;
import com.job.client.model.rabbitmq.AuthConstant;
import com.job.feign.admin.audit.AuditFeign;
import com.job.admin.model.audit.constant.StatusConstant;
import com.job.admin.model.audit.pojo.AuditAvatar;
import com.job.client.auth.service.AuthService;
import com.job.client.common.exception.MyselfException;
import com.job.client.common.result.ResponseResult;
import com.job.client.model.auth.pojo.User;
import com.job.client.model.rabbitmq.AuditResultConstant;
import com.job.client.model.rabbitmq.MessageConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Component
@Slf4j
public class AuthListener {
    @Autowired
    private AuditFeign auditFeign;
    @Autowired
    private AuthService authService;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private ActionMapper actionMapper;

    @RabbitListener(queues = AuditResultConstant.QUEUE_AUDIT_AVATAR_RESULT)
    public void setUserAvatar(String dataJSON){
        HashMap<String,String> data = JSON.parseObject(dataJSON, HashMap.class);
        String avatarId = data.get("avatarId");
        ResponseResult result = auditFeign.getAvatarResultById(avatarId);
        if(result.getCode()!=200){
            new MyselfException(201,"接收头像审核结果的数据报错",null);
            return;
        }
        AuditAvatar auditAvatar = JSON.parseObject(JSON.toJSONString(result.getData()),AuditAvatar.class);
        // 整合rabbitmq发送的数据
        HashMap<String,String> message = new HashMap<>();
        message.put("userId",auditAvatar.getAvatarUser()); // 用户的id
        message.put("result",data.get("result")); // 是否审核通过

        if(StatusConstant.STATUS_AUDIT_SUCCESS.equals(Integer.valueOf(data.get("result")))){ //审核头像成功
            User user = new User(auditAvatar.getAvatarUser(),auditAvatar.getAvatarUrl());
            ResponseResult updated = authService.updateAvatar(user);
            if(updated.getCode()!=200){
                new MyselfException(201,"接收头像审核结果的修改头像报错",null);
            }
        }else{
            message.put("reason",auditAvatar.getReason()); // 审核未通过的理由
        }
        // 发送rabbitmq
        rabbitTemplate.convertAndSend(
                MessageConstant.EXCHANGE_MESSAGE,
                MessageConstant.ROUTINGKEY_AVATAR,
                JSON.toJSONString(message));
    }

    @RabbitListener(queues = AuthConstant.QUEUE_AUTH_ACTION)
    public void setUserAction(String dataJSON){
        HashMap<String,String> data = JSON.parseObject(dataJSON, HashMap.class);
        String userId = data.get("userId");
        Integer actionType = Integer.valueOf(data.get("actionType"));
        if(userId == null || actionType == null){
            log.error("queue_auth_action获得数据失败");
        }
        // 把结果传输到数据库中
        Action action = new Action();
        action.setUserId(userId);
        action.setActionType(actionType);
        int i = actionMapper.insert(action);
        if(i != 1){
            log.error("报错用户行为日志失败");
        }
    }
}
